<template>
  <div class="page-miH page-bgBack">
    <div style="margin: 0 auto" :class="status === 'sendResume' ? '' : status !== 'look' ? 'width1000' : ''">
      <ModuleShow
        ref="basic"
        :title="'员工基本信息'"
        v-if="isShow"
        :status="status"
        :workYearsNoExperience="workYearsNoExperience"
        :dataSource="basicData"
        :editable.sync="basicInfoFormJson.editable"
        :dynamicData="{}"
        :formFields="basicInfoFormJson.fields"
        :sortedKeys="basicInfoFormJson.sortedKeys"
        :intentionJobAll="intentionJobAll"
        :formConfig="{}"
        class="pt16"
        style="margin: 0 auto"
        @itemChange="itemChangebasic"
        @change="dynamicChange($event, 'basic', 'basicInfoFormJson', null)"
        @edit="editItem('basicInfoFormJson')"
        @delete="deleteItem('basicInfoFormJson')"
        @cancel="cancelItem('basicInfoFormJson')"
      />
    </div>
  </div>
</template>
<script>
import ModuleShow from "./moduleShow";
import {
  findBySiteCustomTemplates,
  findByTemplateId,
  selectByDefaultAll,
  getTalentPoolIdInfo,
  selectOneByTypeAndId,
  resumeTransformation
} from "./api";
export default {
  components: { ModuleShow },
  props: {
    status: {
      type: String,
      default: ""
    },
    companyUnitsInfo: {
      type: Object,
      default: () => {}
    }
  },
  watch: {
    status: {
      handler(val) {
        if (val) {
          this.basicInfo();
        }
      },
      immediate: true,
      deep: true
    }
  },
  data() {
    return {
      workYearsNoExperience: false,
      isShow: true,
      detailedShow: true,
      loading: false,
      addEmploymentInfoValShow: true,
      empId: "",
      employmentList: [],
      basicData: null,
      detailedData: {},
      employmentDate: {},
      basicInfoFormJson: {},
      detailedFormJson: {},
      employmentFormJson: {},
      authorityArr: [],
      deleteitem: false,
      employeeVO: {},
      employeeRecords: {},
      disabledDate: null,
      dataList: [],
      intentionJobAll: "",
      afterConversionAnnex: null
    };
  },

  methods: {
    addEmploymentInfoVal(text) {
      this.employeeRecords = {};
      if (this.employmentList.length === 0) {
        this.employmentList.push({
          idNum: 0,
          editable: true,
          fields: this.employmentFormJson.fields,
          sortedKeys: this.employmentFormJson.sortedKeys
        });
      } else {
        if (!text && this.status !== "add") {
          this.employmentList.filter((z) => (z.editable = false));
        }
        this.employmentList.push({
          idNum: this.employmentList[this.employmentList.length - 1].idNum + 1,
          editable: true,
          fields: this.employmentFormJson.fields,
          sortedKeys: this.employmentFormJson.sortedKeys
        });
      }
      if (this.status !== "add") {
        this.addEmploymentInfoValShow = !!text;
      }
    },
    dynamicRemove(id, index) {
      this.employmentList.splice(index, 1);
      this.addEmploymentInfoValShow = true;
    },
    dynamicChange(value, sign, edit, idNum) {
      Object.keys(value).map((z) => {
        if (Array.isArray(value[z]) && value[z].length > 0 && typeof value[z][0] === "string" && value[z][0].indexOf("https://") !== -1) {
          value[z] = value[z] + "";
        }
        value[z] = value[z] === undefined ? null : value[z];
      });
      this.$emit("handleSubmit", { afterConversionAnnex: this.afterConversionAnnex, ...value });
    },
    getBirth(idCard) {
      var birthday = "";
      if (idCard !== null && idCard !== "") {
        if (idCard.length === 15) {
          birthday = "19" + idCard.slice(6, 12);
        } else if (idCard.length === 18) {
          birthday = idCard.slice(6, 14);
        }
        birthday = birthday.replace(/(.{4})(.{2})/, "$1-$2-");
      }
      return birthday;
    },
    getSex(idCard) {
      var sexStr = "";
      if (parseInt(idCard.slice(-2, -1)) % 2 === 1) {
        sexStr = "1";
      } else {
        sexStr = "2";
      }
      return sexStr;
    },
    GetDateDiff(time1, time2) {
      time1 = new Date(time1); // 转换为中国标准时间
      time2 = new Date(time2);
      time1 = time1.getTime(); // 转换为时间戳
      time2 = time2.getTime();
      var runTime = (time2 - time1) / 1000; // 开始得出时间差,然后计算
      var year = Math.floor(runTime / 86400 / 365);
      runTime = runTime % (86400 * 365);
      var month = Math.floor(runTime / 86400 / 30);
      runTime = runTime % (86400 * 30);
      var day = Math.floor(runTime / 86400);
      runTime = runTime % 86400;
      var hour = Math.floor(runTime / 3600);
      runTime = runTime % 3600;
      var minute = Math.floor(runTime / 60);
      runTime = runTime % 60;
      var second = runTime;
      return year + "," + month + "," + day;
    },
    itemChangebasic(val, name, dataSource) {
      console.log("12", val, name, dataSource);
      if (name === "annex" && val[0].status === "done") {
        resumeTransformation({ filePath: val[0].url }).then((res) => {
          if (res.data.transformationResultUrl) {
            this.afterConversionAnnex = res.data.transformationResultUrl;
          }
        });
      }
    },
    getTalentPoolInfoFun() {
      getTalentPoolIdInfo(this.companyUnitsInfo.talentPoolId).then((res) => {
        const basicData = res.data;
        Object.keys(basicData).map((z) => {
          if (typeof basicData[z] === "string" && basicData[z].indexOf("https://") !== -1) {
            basicData[z] = basicData[z].split(",");
          }
        });
        this.basicData = basicData;
        if ((this.status === "look" || this.status === "deliveryRecord") && this.basicData.intentionJob) {
          this.basicData.intentionJob = this.basicData.intentionJob.split("*")[1];
        } else if ((this.status === "edit" || this.status === "sendResume") && this.basicData.intentionJob) {
          this.intentionJobAll = this.basicData.intentionJob;
          const intentionJob = this.basicData.intentionJob.split("*");
          const ids = intentionJob[0].split(",");
          const text = intentionJob[1].split(",");
          const arr = text.map((i, index) => ids[index * 3 + 2]);
          this.basicData.intentionJob = arr;
          this.$refs.basic.setFieldsValueShow("intentionJob", arr);
        }
        if ((this.status === "edit" || this.status === "sendResume") && this.basicData.skill) {
          const skill = this.basicData.skill.split(",");
          this.basicData.skill = skill;
          this.$refs.basic.setFieldsValueShow("skill", skill);
        }
        if ((this.status === "edit" || this.status === "sendResume") && this.basicData.workYears) {
          this.workYearsNoExperience = !!(this.basicData.workYears === 0 || this.basicData.workYears === "0");
          this.basicData.workYears = Number(this.basicData.workYears);
        }
      });
    },
    async basicInfo() {
      // this.status === "sendResume" 人才库投递简历
      const results = await selectByDefaultAll();
      this.basicInfoFormJson = {
        editable: this.status === "add" || this.status === "edit" || this.status === "sendResume",
        fields: {},
        sortedKeys: []
      };
      let require = null;
      if (this.status === "add") {
        require = findBySiteCustomTemplates({ type: "recruit2Input2" });
      } else if (this.status === "deliveryRecord" || this.status === "sendResume") {
        require = selectOneByTypeAndId({
          type: "recruit2Delivery2",
          subjectType: "recruit",
          subjectId: this.companyUnitsInfo.recruitInfoId
        });
      } else {
        require = findByTemplateId(results.data.find((z) => z.type === "recruit2SelectResult2").id);
      }
      require
        .then((res) => {
          let data =
            this.status === "deliveryRecord" || this.status === "add" || this.status === "sendResume"
              ? res.data.customTemplateFieldListVOList
              : res.data;
          data = data.map((z) => {
            z.fieldCode = z.enName;
            this.basicInfoFormJson.fields[z.enName] = JSON.parse(z.formJson)[z.enName];
            return z;
          });
          if (this.basicInfoFormJson.fields.addressCode) {
            this.basicInfoFormJson.fields.addressCode.extend.level = 2;
          }
          if (this.basicInfoFormJson.fields.annex) {
            // 上传简历只能上传一张
            this.basicInfoFormJson.fields.annex.extend.limit = 1;
            this.basicInfoFormJson.fields.annex.customize.accept = ".doc, .docx,.pdf,.jpg, .jpeg, .png";
          }
          this.basicInfoFormJson.sortedKeys = data.map((z) => z.enName);
          if (this.status !== "add") {
            this.getTalentPoolInfoFun();
          } else {
            this.basicData = {};
          }
          console.log("123", this.status);
          if ((this.status === "edit" || this.status === "sendResume") && this.basicInfoFormJson.fields.phone) {
            this.basicInfoFormJson.fields.phone.customize.disabled = true;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    goBack() {
      this.$router.go(-1);
    },
    editItem(sign, item) {
      this[sign].editable = true;
      this.addEmploymentInfoValShow = false;
    },
    cancelItem(sign, item) {
      console.log(sign, item, this.status);
      if (this.status === "sendResume") {
        this.$emit("sendResume");
      } else if (this.status === "edit") {
        this.$emit("editCancel");
      }
    },
    deleteItem(sign, item) {
      console.log(sign, item);
    }
  }
};
</script>
<style lang="scss" scoped>
.boxBottom {
  width: 100%;
  height: 56px;
  line-height: 56px;
  background: #ffffff;
  text-align: center;
  box-shadow: 0px 0px 7px 0px rgba(0, 0, 0, 0.15);
  position: fixed;
  bottom: 0;
  padding-right: 204px;
  left: 207px;
}
.bore8 {
  padding-bottom: 5px;
  // border-bottom: 1px solid #e8e8e8;
}
.peopleBoxCon1 .bore8:nth-last-child(1) {
  border-bottom: 0;
}
.width1000 {
  width: 1000px;
}
</style>
